"""
MCP (Model Context Protocol) 核心数据模型
定义工具、资源和提示的标准化数据结构
"""
from typing import Any, Dict, List, Optional, Union
from pydantic import BaseModel, Field
from enum import Enum


class ControlLevel(str, Enum):
    """控制层级枚举"""
    USER = "user"           # 用户控制
    APPLICATION = "application"  # 应用控制
    MODEL = "model"         # 模型控制


class ResourceType(str, Enum):
    """资源类型枚举"""
    TEXT = "text"
    BINARY = "binary"
    JSON = "json"
    FILE = "file"


class ToolParameter(BaseModel):
    """工具参数定义"""
    name: str
    type: str
    description: Optional[str] = None
    required: bool = True
    default: Optional[Any] = None


class Tool(BaseModel):
    """工具定义 - 可执行的功能"""
    name: str
    description: str
    parameters: List[ToolParameter] = Field(default_factory=list)
    control_level: ControlLevel = ControlLevel.MODEL
    
    class Config:
        json_schema_extra = {
            "example": {
                "name": "send_notification",
                "description": "发送通知消息",
                "parameters": [
                    {
                        "name": "message",
                        "type": "string",
                        "description": "通知内容",
                        "required": True
                    },
                    {
                        "name": "priority",
                        "type": "string",
                        "description": "优先级",
                        "required": False,
                        "default": "normal"
                    }
                ]
            }
        }


class Resource(BaseModel):
    """资源定义 - 结构化数据流"""
    uri: str
    name: str
    description: str
    type: ResourceType
    control_level: ControlLevel = ControlLevel.APPLICATION
    template_params: Optional[Dict[str, str]] = None
    
    class Config:
        json_schema_extra = {
            "example": {
                "uri": "file://logs/{date}.log",
                "name": "daily_logs",
                "description": "每日系统日志",
                "type": "text",
                "template_params": {"date": "YYYY-MM-DD"}
            }
        }


class Prompt(BaseModel):
    """提示定义 - 可重用的指令模板"""
    name: str
    description: str
    template: str
    parameters: List[ToolParameter] = Field(default_factory=list)
    control_level: ControlLevel = ControlLevel.USER
    
    class Config:
        json_schema_extra = {
            "example": {
                "name": "code_review",
                "description": "代码审查提示模板",
                "template": "请审查以下代码：{code}\n重点关注：{focus_areas}",
                "parameters": [
                    {
                        "name": "code",
                        "type": "string",
                        "description": "要审查的代码",
                        "required": True
                    },
                    {
                        "name": "focus_areas",
                        "type": "string",
                        "description": "审查重点",
                        "required": False,
                        "default": "安全性、性能、可维护性"
                    }
                ]
            }
        }


class ToolCall(BaseModel):
    """工具调用请求"""
    tool_name: str
    parameters: Dict[str, Any]
    request_id: Optional[str] = None


class ToolResult(BaseModel):
    """工具执行结果"""
    request_id: Optional[str] = None
    success: bool
    result: Optional[Any] = None
    error: Optional[str] = None


class ResourceRequest(BaseModel):
    """资源请求"""
    uri: str
    parameters: Optional[Dict[str, Any]] = None


class ResourceResponse(BaseModel):
    """资源响应"""
    uri: str
    content: Union[str, bytes]
    content_type: str
    metadata: Optional[Dict[str, Any]] = None


class PromptRequest(BaseModel):
    """提示请求"""
    name: str
    parameters: Dict[str, Any]


class PromptResponse(BaseModel):
    """提示响应"""
    name: str
    rendered_template: str
    metadata: Optional[Dict[str, Any]] = None


class MCPCapabilities(BaseModel):
    """MCP服务器能力声明"""
    tools: List[Tool] = Field(default_factory=list)
    resources: List[Resource] = Field(default_factory=list)
    prompts: List[Prompt] = Field(default_factory=list)
    server_info: Optional[Dict[str, Any]] = None 